<!-- find页面 日签 -->
<template>
  <div class="dailySignatureBox" ref="dailybox" @scroll="addData">
    <div class="dailyTitle">
      <div
        class="dailyList"
        v-for="(item, index) in data"
        :key="index"
        @click="goPage(item.maketime.substr(0, 10))"
      >
        <div class="top" v-if="item.top">
          <van-divider @click.stop>{{ item.top }}月</van-divider>
        </div>
        <div class="listdata" v-if="item.title">
          <img :src="item.cover" alt="" />
          <div>{{ item.maketime.substr(0, 10) }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getRead } from "@/api/find.js";
export default {
  name: "dailySignature",
  props: ["newYear", "newMonth", "date"],
  data() {
    return {
      data: [],
      now: null,
      year: 0,
      month: 0,
      day: 0,
      topmounth: null,
      oldYear: 0,
      oldMonth: 0,
    };
  },
  created() {
    this.getDailyData();
    console.log(this.date);
  },
  methods: {
    goPage(time) {
      if (this.date === "date") {
        this.$emit("changeDate", time);
      } else {
        this.$router.push(`/imageText/${time}`);
      }
    },
    async getDailyData() {
      this.now = new Date(); //获取带有格式的时间
      this.year = this.newYear;
      this.month = this.newMonth; //月份从0开始，需要加1
      this.day = this.now.getDate();
      console.log("当前年月日", this.year, this.month, this.day);
      const { data } = await getRead(
        0,
        this.year + "-" + (this.month < 10 ? "0" + this.month : this.month)
      );
      this.data = data;
      console.log("获取的数据", this.data, this.data[0].maketime.substr(5, 2));
      this.data.unshift({ top: this.data[0].maketime.substr(5, 2) });
      //  console.log('父组件传进来的数据',this.newYear,this.newMonth);
    },
    addData() {
      //scrollTop就是触发滚轮事件时滚轮的高度
      // let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      let scrollTop = this.$refs.dailybox.scrollTop;
      let scrollHeight = this.$refs.dailybox.scrollHeight; //滚动条的高度
      let clientHeight = this.$refs.dailybox.clientHeight; //浏览器的可视高度
      console.log("scrollTop", scrollTop);
      console.log(
        "滚动条高度",
        scrollHeight,
        "浏览器可视高度",
        scrollTop + clientHeight + 100
      );
      if (scrollTop + clientHeight >= scrollHeight) {
        if (this.timer) {
          clearTimeout(this.timer);
        }
        this.timer = setTimeout(() => {
          this.getLoading();
        }, 1000);
        console.log("触底");
      }
    },
    async getLoading() {
      if (parseInt(this.data[this.data.length - 1].maketime.substr(5, 2)) < 2) {
        this.oldYear =
          parseInt(this.data[this.data.length - 1].maketime.substr(0, 4)) - 1;
        this.oldMonth = "12";
      } else {
        this.oldYear = parseInt(
          this.data[this.data.length - 1].maketime.substr(0, 4)
        );
        this.oldMonth =
          parseInt(this.data[this.data.length - 1].maketime.substr(5, 2)) - 1 <
          10
            ? "0" +
              (parseInt(this.data[this.data.length - 1].maketime.substr(5, 2)) -
                1)
            : parseInt(this.data[this.data.length - 1].maketime.substr(5, 2)) -
              1;
      }
      const { data } = await getRead(0, this.oldYear + "-" + this.oldMonth);
      console.log("年月份", this.oldYear, this.oldMonth);
      console.log(
        "裁剪的数据",
        parseInt(this.data[this.data.length - 1].maketime.substr(5, 2)) - 1
      );
      // 每组数据前添加个月份top，用来展示
      this.data.push({ top: data[data.length - 1].maketime.substr(5, 2) });
      this.data.push(...data);
      console.log("新数据", this.data);
    },
  },
};
</script>

<style lang="less" scoped>
.dailySignatureBox {
  overflow: scroll;
  max-height: 667px;
  padding-top: 40px;
  padding: 0 15px;
  background: #fcfcfc;

  .dailyTitle {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    .dailyList {
      .top {
        width: 345px;
        flex-grow: 1;
      }
      .listdata {
        width: 165px;
        height: 120px;
        margin-bottom: 15px;
        text-align: center;
        background: #ffffff;
        border-radius: 5px;
        outline: solid;
        outline-width: 1px;
        outline-color: #e6e6e6;
        color: #808080;
        font-size: 12px;
        img {
          width: 100%;
          height: 100px;
          border-top-left-radius: 5px;
          border-top-right-radius: 5px;
          display: block;
        }
        & > :nth-child(2) {
          padding: 5px 0;
        }
      }
    }
  }
}
</style>